package com.xh.bussiness.crm.task;

import com.xh.bussiness.crm.bean.request.ClueQueryRequest;
import com.xh.bussiness.crm.service.IClueService;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.util.DateUtils;
import com.xh.core.util.EmailUtils;
import com.xh.core.util.FileUtils;
import com.xh.core.util.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.LinkedHashMap;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/6/3 16:28
 **/
@Component
public class ClueDailyReportTask {

    @Autowired
    private IClueService clueService;

 //   @Scheduled(cron = "0/5 * * * * ?")
//    @Scheduled(cron = "0 0 9 * * ?")
    private void clueReportSend() {
        LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
        exportHeaderBeans.put("schoolName",new XhExportHeaderBean("学校名称"));
        exportHeaderBeans.put("applierName",new XhExportHeaderBean("申请人姓名"));
        exportHeaderBeans.put("applierMobile",new XhExportHeaderBean("申请人手机"));
        exportHeaderBeans.put("source",new XhExportHeaderBean("来源信息"));
        exportHeaderBeans.put("createTime",new XhExportHeaderBean("申请时间","yyyy-MM-dd HH:mm:ss"));

        Workbook workbook=null;
        try {
            ClueQueryRequest queryBean=new ClueQueryRequest();
            workbook=clueService.exportAll(exportHeaderBeans,queryBean,"queryYestodayPageList");
            String yestoday=DateUtils.format(DateUtils.dateAddForDay(new Date(),-1),"yyyy-MM-dd");
            if(workbook==null){
                EmailUtils.sendSimpleEmail("爱学每日线索（昨日无线索）", "爱学每日线索数据"+yestoday+"无线索", "40087783@qq.com", "daiwei@xiaohe.com,hanxiaodong@xiaohe.com,wanglihong@xiaohe.com,xulian@xiaohe.com");
                return;
            }
            ApplicationHome home=new ApplicationHome(getClass());
            File jarFile=home.getSource();
            String savePath=jarFile.getParentFile().getParentFile().getAbsolutePath()+"/temp/";
            String saveFile=savePath+FileUtils.createRandomFilename()+".xlsx";

            File savefile = new File(savePath);
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(saveFile);
            workbook.write(fos);
            fos.close();
            EmailUtils.sendEnclosureEmail("爱学每日线索", "爱学每日线索数据"+yestoday, "40087783@qq.com", "daiwei@xiaohe.com,hanxiaodong@xiaohe.com,wanglihong@xiaohe.com,xulian@xiaohe.com",
                "爱学每日线索-"+yestoday+".xlsx",saveFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
